<template>
	<view>
		<page title="忘记密码" bgClass="bg-white">
			
			<view class="qui-cells">
				<view class="qui-cell bor-left_0">
					<view class="w180">手机号码：</view>
					<view class="cell-bd"><input type="text" v-model="form.mobile" value="" placeholder="请输入手机账号" /></view>
				</view>
				<view class="qui-cell bor-left_0">
					<view class="w180">验证码：</view>
					<view class="cell-bd"><input type="text" v-model="form.mobile_code" value="" placeholder="请输入手机验证码" /></view>
					<view class="qui-btn w160 bg-gray inline small"><send-code type="2" :tel="form.mobile"></send-code></view>
				</view>
				<view class="qui-cell bor-left_0">
					<view class="w180">新密码：</view>
					<view class="cell-bd"><input type="text" v-model="form.password" value="" placeholder="请设置你的新密码" /></view>
				</view>
				<view class="qui-cell bor-left_0">
					<view class="w180">确认密码：</view>
					<view class="cell-bd"><input type="text" v-model="form.repassword" value="" placeholder="请输入确认密码" /></view>
				</view>
				<view class="h60"></view>
				<view class="plr50 mt50">
					<view @click="submit" class="qui-btn round">确定</view>
				</view>
			</view>
			
		</page>
	
	    <uni-popup></uni-popup>
	</view>
</template>

<script>
	import sendCode from '@/components/send-code.vue';
	export default {
		components:{
			sendCode
		},
		data() {
			return {
				form: {
					password:'',
					repassword: '',
					mobile: '',
					mobile_code: ''
				}
			}
		},
		onLoad() {
			this.isLogin(false)
		},
		methods: {
			async resetPassword() {
				const res = await this.post('/api/user/reset_password', this.form)
				this.success(res, '密码修改成功,重新登录~', function() {
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/login/login'
						})
					},1000)
				});
			},
			submit() {
				let vRules = [
					{ name: 'mobile', required: true, type: 'phone', errmsg: '请输入正确的手机号' },
					{ name: 'password', type: 'required', errmsg: '请输入密码' },
					{name: 'repassword', type: 'required', errmsg: '请输入确认密码'},
					{name: 'password', type: 'eq', eqName: 'repassword', required: true, errmsg: '两次输入密码不一致'},
					{ name: 'mobile_code', type: 'required', errmsg: '请输入验证码' },
				];
				
				let vRes = this.$validate.validate(this.form, vRules);
				
				if (!vRes.isOk) {
					uni.showToast({
						icon: 'none',
						title: vRes.errmsg
					});
					return false;
				}
				this.resetPassword()
			}
		}
	}
</script>

<style>

</style>
